package com.start.data.screen.aspects;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.start.data.screen.StartDataScreenAutoConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DataScreenAspects {

    @Autowired
    private StartDataScreenAutoConfiguration startDataScreenAutoConfiguration;

    @Pointcut("execution(public * com.start.data.screen.service.impl.DataScreen*(..))")
    public void dataScreenAspectsPointcut() {
    }

    @Before("dataScreenAspectsPointcut()")
    public void before(ProceedingJoinPoint point) {
        String dataSource = startDataScreenAutoConfiguration.getDataSource();
        if (StringUtils.isEmpty(dataSource)) {
            return;
        }
        DynamicDataSourceContextHolder.push(dataSource);
    }

    @After("dataScreenAspectsPointcut()")
    public void after() {
        DynamicDataSourceContextHolder.clear();
    }
}
